<?php

namespace app\admin\model;

class DataClass
{
    public static function canyu($wheremap)
    {
        $map = array_merge_recursive($wheremap,whereSchoolMap(false));
        $zong_num = SportRecord::where($map)->group('memberId')->count('*');
        $zong_time = SportRecord::where($map)->sum('duration') ;
        $zong_avg = SportRecord::where($map)->avg('duration') ;

        $today = date('Y-m-d'); // 当前日期
        $sevenDaysAgo = date('Y-m-d', strtotime('-7 days')); // 7天前的日期

        $dateArray = array(); // 存储日期数组的变量

        for ($date = $sevenDaysAgo; $date <= $today; $date = date('Y-m-d', strtotime('+1 day', strtotime($date)))) {
            $dateArray[] = $date;
        }

        $arr = [];
        foreach($dateArray as $k=>$v){
            $arr[$k]['time'] = date('m-d',strtotime($v));
            $t1 = strtotime($v);
            $t2 = date('Y-m-d H:i:s', $t1+24*3600);
            $rmap = [];
            $rmap[] = ['createdDate',['>',$v],['<',$t2]];
            $rmap = array_merge_recursive($rmap,$wheremap);
            $rmap = array_merge_recursive($rmap,whereSchoolMap(false));
            $v1 = SportRecord::where($rmap)->group('memberId')->count('*');
            $v2 =  SportRecord::where($rmap)->sum('duration') ;
            $arr[$k]['visits'] =   (int)$v1;
            $arr[$k]['views'] = (int)$v2;
        }

        $list = [
            'zong_num' => (int)$zong_num,
            'zong_time' => (int)($zong_time/60).'分钟',
            'zong_avg' => (int)$zong_avg,
            'canyu_data' => $arr,
        ];
        return $list;
    }

    //排名
        /*  ['id' => 12,'rank'=> 1,'no' => 20230340,'name' => '张小虎1','num' => '32.324',],
                    ['id' => 12,'rank'=> 2,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                    ['id' => 12,'rank'=> 3,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                    ['id' => 12,'rank'=> 4,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                    ['id' => 12,'rank'=> 5,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
    ]*/
    public static function rank()
    {
        $list1 = [];
        $list2 = [];
        return [$list1,$list2];
    }
    public static function feihuoCount($id,$map1,$gender = ''){
        $map=[];
        if ($id) {
            $map[] = ['classId','=',$id];
        }
        
        $map[] = ['itemCode','=','vitalCapac'];
        $res = self::countBmi(array_merge_recursive($map,$map1),$gender);
        return $res;
    }
    //肺活量
    public static function manFeihuo($id,$gender = '')
    {
        //男,优秀 大于 4800  良好 4300-4800 及格 3100-4300 不及格 小于3100
        $yx = self::feihuoCount($id,[['score','>=',4800]],1);
        $lh = self::feihuoCount($id,[['score',['>=',4300],['<',4800]]],1);
        $jg = self::feihuoCount($id,[['score',['>=',3100],['<',4300]]],1);
        $nojg = self::feihuoCount($id,[['score','<',3100]],1);
        $total = $yx+$lh+$jg+$nojg;
        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        if($total>0){
            $rate1 = cacl_rate($yx,$total);
            $rate2 = cacl_rate($lh,$total);
            $rate3 = cacl_rate($jg,$total);
            $rate4 = cacl_rate($nojg,$total);
        }
        $res = [
            [ 'value'=> $rate1, 'name'=>'优秀',],
            [ 'value'=> $rate2, 'name'=>'良好',],
            [ 'value'=> $rate3, 'name'=>'及格',],
            [ 'value'=> $rate4, 'name'=>'不及格',]
        ];
        return $res;
        //女,优秀 大于 3200  良好 3000-3200 及格 2000-3000 不及格 小于2000
    }

    //肺活量
    public static function womainFeihuo($id,$gender = '')
    {
        //女,优秀 大于 3200  良好 3000-3200 及格 2000-3000 不及格 小于 2000
        $yx = self::feihuoCount($id,[['score','>=',3200]],2);
        $lh = self::feihuoCount($id,[['score',['<=',3000],['>',3200]]],2);
        $jg = self::feihuoCount($id,[['score',['<=',2000],['>',3000]]],2);
        $nojg = self::feihuoCount($id,[['score','<',2000]],2);
        $total = $yx+$lh+$jg+$nojg;
        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        if($total>0){
            $rate1 = cacl_rate($yx,$total);
            $rate2 = cacl_rate($lh,$total);
            $rate3 = cacl_rate($jg,$total);
            $rate4 = cacl_rate($nojg,$total);
        }
        $res = [
            [ 'value'=> $rate1, 'name'=>'优秀',],
            [ 'value'=> $rate2, 'name'=>'良好',],
            [ 'value'=> $rate3, 'name'=>'及格',],
            [ 'value'=> $rate4, 'name'=>'不及格',]
        ];
        return $res;

    }

    public static function feihuo($id)
    {
        $yx1 = self::feihuoCount($id,[['score','>=',4800]],1);
        $lh1 = self::feihuoCount($id,[['score',['>=',4300],['<',4800]]],1);
        $jg1 = self::feihuoCount($id,[['score',['>=',3100],['<',4300]]],1);
        $nojg1 = self::feihuoCount($id,[['score','<',3100]],1);
        $yx2 = self::feihuoCount($id,[['score','>=',3200]],2);
        $lh2 = self::feihuoCount($id,[['score',['>=',3000],['<',3200]]],2);
        $jg2 = self::feihuoCount($id,[['score',['>=',2000],['<',3000]]],2);
        $nojg2 = self::feihuoCount($id,[['score','<',2000]],2);
        $total = $yx1+$lh1+$jg1+$nojg1+$yx2+$lh2+$jg2+$nojg2;
        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        if($total>0){
            $rate1 = cacl_rate($yx1+$yx2,$total);
            $rate2 = cacl_rate($lh1+$lh2,$total);
            $rate3 = cacl_rate($jg1+$jg2,$total);
            $rate4 = cacl_rate($nojg1+$nojg2,$total);
        }
        $res = [
            [ 'value'=> $rate1, 'name'=>'优秀',],
            [ 'value'=> $rate2, 'name'=>'良好',],
            [ 'value'=> $rate3, 'name'=>'及格',],
            [ 'value'=> $rate4, 'name'=>'不及格',]
        ];
        return $res;
    }

    public static function countBmi($map,$gender = '')
    {
        $map = array_merge_recursive($map,whereSchoolMap(false));
        if($gender){
            $map[] = ['gender' , '=' , $gender];
        }
        $count = SportRecord::where($map)
            ->group('memberId')
            //->whereNull('bmi')
            ->count();
        return $count;
    }

    public static function selectBmi($departId,$gender = '')
    {
        $row = Standard::where('name','BMI指数')->find();
        if($row){
            /* $sta = $row['standard'];
             print_r($sta);*/
            //男 偏瘦 小于 17.9 正常17.9-24 超重24 - 28 肥胖>28
             if ($departId) {
                 $idarr = ['classId','=',$departId];
             }else{
                $idarr = ['1','=','1'];
             }
            $ps = self::countBmi([['bmi','<',17.9],$idarr],$gender);
            $zc = self::countBmi([['bmi',['>=',17.9],['<=',24]],$idarr],$gender);
            $cz = self::countBmi([['bmi',['>',24],['<=',28]],$idarr],$gender);
            $fp = self::countBmi([['bmi','>',28],$idarr],$gender);

            // 女 偏瘦 小于17.9 正常17.9-24 超重24 - 28  肥胖>28
            $total = $ps+$zc+$cz+$fp;
            //echo $total;
            $rate1 = 0;
            $rate2 = 0;
            $rate3 = 0;
            $rate4 = 0;
            if($total>0){
                $rate1 = cacl_rate($ps,$total);
                $rate2 = cacl_rate($zc,$total);
                $rate3 = cacl_rate($cz,$total);
                $rate4 = cacl_rate($fp,$total);
            }
            $res = [
                [ 'value'=> $rate1, 'name'=>'偏瘦',],
                [ 'value'=> $rate2, 'name'=>'正常',],
                [ 'value'=> $rate3, 'name'=>'过量',],
                [ 'value'=> $rate4, 'name'=>'肥胖',]
            ];
            return $res;
        }else{
            $res = [
                [ 'value'=> 0, 'name'=>'偏瘦',],
                [ 'value'=> 0, 'name'=>'正常',],
                [ 'value'=> 0, 'name'=>'过量',],
                [ 'value'=> 0, 'name'=>'肥胖',]
            ];
            return $res;
        }
    }
}